<?php
namespace app\admin\model;
use think\Model;
class Config extends Model{
	
	/**
     * 从系统配置表中取出模块相关配置
     * @author admin
     * @date 2018-3-25
     * @return array
     */
    static public function getCfgByModule($module_name){
		$data=Config::where("c_key",$module_name)->find();
        return json_decode($data['c_value'],true);
    }
    
    /**
     * 保存配置
     * @author billow.wang<admin@ivears.com>
     * @date 2015-05-14
     * @param string $key 配置项
     * @param string $value 配置值
     * @param string $desc 配置项描述，为空则不修改描述。修改配置项时一般不用修改描述的
     */
    static public function setConfig($key,$value,$desc='',$status=1){
    	$cfg = Config::where("c_key",$key)->find();
    	if($cfg){
    		$data = array(
    			'c_key' => $key,
    			'c_value' => $value,
    			'c_value_desc' => $desc,
				'status'=>$status,
    			'c_update_time' => time()
    		);
            if(empty($desc)){
                unset($data['c_value_desc']);
            }
			return Config::where("id",$cfg['id'])->update($data);
    	}else{
    		$data = array(
    			'c_key' => $key,
    			'c_value' => $value,
    			'c_value_desc' => $desc,
				'status'=>$status,
    			'c_create_time' => time(),
				'c_update_time' => time()
    		);
            if(empty($desc)){
                unset($data['c_value_desc']);
            }
			return Config::create($data);
    	}
    }

}